package com.lz.yz.reward.service.impl;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.base.util.Constant;
import com.lz.yz.core.AbstractService;
import com.lz.yz.member.service.PropertyInfoService;
import com.lz.yz.order.enumeration.TurnoversType;
import com.lz.yz.order.service.TurnoversService;
import com.lz.yz.outing.service.SyncLockService;
import com.lz.yz.reward.bean.RewardBean;
import com.lz.yz.reward.dao.AnswerMapper;
import com.lz.yz.reward.dao.RewardMapper;
import com.lz.yz.reward.entity.Answer;
import com.lz.yz.reward.entity.Reward;
import com.lz.yz.reward.enumeration.RewardStatus;
import com.lz.yz.reward.model.AnswerModel;
import com.lz.yz.reward.model.RewardModel;
import com.lz.yz.reward.param.RewardParam;
import com.lz.yz.reward.service.AnswerService;
import com.lz.yz.reward.service.RewardService;
import com.util.common.Page;
import com.util.common.QueryList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by CodeGenerator on 2017/11/14.
 */
@Service
@Transactional
public class AnswerServiceImpl extends AbstractService<Answer> implements AnswerService {
    @Resource
    private AnswerMapper answerMapper;

    @Autowired
    SyncLockService syncLockService;

    @Autowired
    RewardMapper rewardMapper;

    @Autowired
    PropertyInfoService propertyInfoService;

    @Autowired
    TurnoversService turnoversService;


    @Override
    public ReturnValue add(AnswerModel model) {
        //控制双重点击事件
        if (!syncLockService.syncLock("addAnswer/" + model.getAccountId(), 1, TimeUnit.SECONDS)) {
            return ReturnValue.generateFalseReturnValue("请不要重复提交!");
        }
        Answer answer = new Answer();
        BeanUtils.copyProperties(model,answer);
        answerMapper.insertSelective(answer);
        rewardMapper.updateByPrimaryKeySelective(new Reward(model.getRewardId(), RewardStatus.ANSWERED.getStatusValue()));
        return ReturnValue.generateTrueReturnValue("回答成功");
    }

    @Override
    public ReturnValue adoptionAnswer(AnswerModel model) {
        Reward rewardBean = rewardMapper.selectByPrimaryKey(model.getRewardId());
        if (rewardBean ==null){
            return ReturnValue.generateFalseReturnValue("找大咖悬赏id无效!");
        }
        Answer answer = answerMapper.selectByPrimaryKey(model.getAnswerId());
        if (answer ==null){
            return ReturnValue.generateFalseReturnValue("回答id无效!");
        }

        rewardMapper.updateByPrimaryKeySelective(new Reward(model.getRewardId(), RewardStatus.COMPLETE.getStatusValue()));

        RewardParam param = new RewardParam();
        param.setRewardId(model.getRewardId());

        //计算众筹的钱给大咖
        List<RewardBean> rewardBeans = rewardMapper.queryAnswerByDate(param);
        if(rewardBeans.size()>0){
            BigDecimal totalPrice = rewardBeans.get(0).getTotalPrice();
            BigDecimal answerPrice; //给大咖的钱
            if(rewardBean.getPrice().compareTo(totalPrice)==1){ //price>TotalPrice
                answerPrice = totalPrice;
            }else {
                answerPrice = rewardBean.getPrice();
            }
            propertyInfoService.increaseBalance(rewardBeans.get(0).getAccountId(),answerPrice);
            turnoversService.insert(rewardBeans.get(0).getAccountId(), TurnoversType.REWARD, Constant.ADDITION,answerPrice,null);
        }
        return ReturnValue.generateTrueReturnValue("采纳成功");
    }

    @Override
    public ReturnValue myAnswer(RewardParam param ) {
        List<RewardBean> rewardBeanList = answerMapper.queryMyAnswers(param);
        QueryList<RewardModel> queryList = new QueryList(rewardBeanList, Page.toPage(rewardBeanList));
        return ReturnValue.generateTrueReturnValue(queryList);
    }
}
